from django.db import models

# Create your models here.
from django.db import models

# Create your models here.
# 我们重写用户模型类, 继承自 AbstractUser
from Administrative_office_system.utils.BaseModel import BaseModel



class HospitalDepartment(BaseModel):
    # 序号	选择	部门编码	部门名称	负责人	副主任	部门属性	电话	传真	地址	邮政编码	电子邮件	信用额度	信用等级	信用天数
    # 批准文号	批准单位	成立日期	撤销日期	备注	部门英文名称	部门全称
    department_code = models.CharField(default='', max_length=50, verbose_name="部门代码")
    name = models.CharField(max_length=255, unique=True, verbose_name='名称')
    principal = models.CharField(max_length=50, verbose_name="部门负责人")


    parent = models.ForeignKey('self', on_delete=models.PROTECT, db_constraint=False, null=True, blank=True, verbose_name='父部门')
    # principal = models.CharField(max_length=50, verbose_name="部门负责人")
    level = models.CharField(default='',max_length=32, verbose_name="级别")

    department_attribute = models.CharField(default='',max_length=50, verbose_name="部门属性")
    incorporation_date = models.CharField(default='',max_length=50, verbose_name="成立日期")
    cancellation_date = models.CharField(default='',max_length=50, verbose_name="撤销日期")
    marks = models.CharField(default='',max_length=50, verbose_name="备注")
    department_english_name = models.CharField(default='',max_length=50, verbose_name="部门英文名称")
    department_full_name = models.CharField(default='',max_length=255, verbose_name="部门全称")


class CustomerForm(BaseModel):
    apply_user = models.ForeignKey(
        'personal_center.User',
        verbose_name="创建人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    name = models.CharField(max_length=255, verbose_name='名称')
    business_html = models.TextField(verbose_name="业务表单html")
    process_html = models.TextField(verbose_name="流程表单html")
    process_json = models.TextField(verbose_name="流程")
    module = models.CharField(max_length=50, verbose_name="模块")
    abandoned = models.CharField(max_length=11, verbose_name="是否废弃", default='0')       #1：废弃， 0：未废弃
    old_flow = models.CharField(max_length=11, verbose_name="是否老流程", default='0')


class CustomerFormModule(BaseModel):
    name = models.CharField(max_length=255, verbose_name='名称')
    abbreviation = models.CharField(max_length=50, verbose_name='简称', default="")


class LowCodeBill(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '审核中'),
        ('two', '结束'),
        ('three', '作废'),
        ('four', '拒绝'),
    )
    apply_user = models.ForeignKey(
        'personal_center.User',
        verbose_name="提交人",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    customer_form = models.ForeignKey(
        'CustomerForm',
        verbose_name="自定义表单模型",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='low_code_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='low_code_already_audit_users', blank=True)
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    data_html = models.TextField(verbose_name="表单数据json")
    check_val = models.CharField('复选框的值', max_length=128, blank=True, default='')
    select_val = models.CharField('下拉框的值', max_length=128, blank=True, default='')


    def get_state_str(self):
        if self.state == 'two':
            return '完结'
        elif self.state == 'four':
            return '退回'
        elif self.state == 'three':
            return '作废'
        else:
            return '审核中'


class LowCodeBillAuditRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '通过'),
        ('unaudit', '待审核'),
    )
    parent_id = models.ForeignKey(
        'LowCodeBill',
        verbose_name="业务单据",
        blank=True,
        null=True,
        on_delete=models.SET_NULL)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey(
        'personal_center.User',
        verbose_name="审批人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10)
    seq = models.IntegerField(default=0, verbose_name='顺序')

# 考勤打卡
class punch_clock(BaseModel):
    TYPE = (
        ("a", "上班"),
        ("b", "下班"),
    )
    WORKSTATUS = (
        ("1", "正常"),
        ("0", "迟到"),
    )
    OFFSTATUS = (
        ("1", "正常"),
        ("0", "早退"),
    )
    state = models.CharField('状态', max_length=10, choices=TYPE, default='')
    clock_people = models.ForeignKey(
        'personal_center.User',
        verbose_name="打卡人",
        blank=False,
        null=False,
        on_delete=models.CASCADE)
    go_work_time = models.CharField(max_length=50, verbose_name='上班时间')
    go_work_status = models.CharField('上班状态', max_length=10, choices=WORKSTATUS, default='1')
    off_work_time = models.CharField(max_length=50, verbose_name='下班时间')
    off_work_status = models.CharField('下班状态', max_length=10, choices=OFFSTATUS, default='1')
    clock_time = models.CharField('打卡日期', max_length=50, default='')


class Payee(BaseModel):
    TYPE = {
        ('service', '使用'),
        ('cancell', '作废')
    }
    payee = models.CharField(max_length=128, verbose_name='收款单位')
    account = models.CharField(max_length=32, verbose_name='收款账户')
    state = models.CharField(max_length=10, choices=TYPE)


class ModuleManagement(BaseModel):
    name = models.CharField(max_length=255, verbose_name='模块名称')
    abbreviation = models.CharField(max_length=50, verbose_name='简称', default="")


class ModuleManagementChildren(BaseModel):
    parent_id = models.ForeignKey('ModuleManagement', related_name='ModuleManagementTable', verbose_name="模块管理", blank=True, null=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=255, verbose_name='模块名称')
    abbreviation = models.CharField(max_length=50, verbose_name='简称', default="")
    flow_chart = models.CharField(max_length=225, verbose_name='流程图地址', default="")